const utils = {
    getScrollOffset() {
        if (window.pageXOffset) {
            return {
                left: window.pageXOffset,
                top: window.pageYOffset
            }
        } else {
            return {
                left: document.body.scrollLeft + document.documentElement.scrollLeft,
                top: document.body.scrollTop + document.documentElement.scrollTop
            }
        }
    },
    pagePos(e) {
        var sLeft = this.getScrollOffset().left,
            sTop = this.getScrollOffset().top,
            cLeft = document.documentElement.clientLeft || 0,
            cTop = document.documentElement.clientTop || 0;

        return {
            X: e.clientX + sLeft - cLeft,
            Y: e.clientY + sTop - cTop,
        }
    },
    setLocalStorage(prop, data) {
        var localStorage = window.localStorage;
        localStorage[prop] = data;
    },
    getLocalStorage(prop) {
        var localStorage = window.localStorage;
        return localStorage[prop];
    },
    setSessionStorage(prop, data) {
        var sessionStorage = window.sessionStorage;
        sessionStorage[prop] = JSON.stringify(data);
    },
    getSessionStorage(prop) {
        var sessionStorage = window.sessionStorage;
        return sessionStorage[prop];
    },
    /**
     * 通过查找数组中的内容 获得同级的其他内容
     我想查找id==1的人的name是什么
    数组、字符串型id、id对应的内容、想查找的字段
    * @param array
    * @param key
    * @param value
    * @param  targetKey
    */
    arrayLookup(data, key, value, targetKey) {
        var targetValue = "";
        for (var i = 0; i < data.length; i++) {
            if (data[i][key] == value) {
                targetValue = data[i][targetKey];
                break;
            }
        }
        return targetValue;
    },
    addEvent(el, type, fn) {
        if (el.addEventListener) {
            el.addEventListener(type, fn, false);
        } else if (el.attachEvent) {
            el.attachEvent('on' + type, function () {
                fn.call(el);
            });
        } else {
            el['on' + type] = fn;
        }
    },
    removeEvent(elem, type, fn) {
        if (elem.addEventListener) {
            elem.removeEventListener(type, fn, false);
        } else if (elem.attachEvent) {
            elem.detachEvent('on' + type, fn);
        } else {
            elem['on' + type] = fn;
        }
    },
    // 取消冒泡
    cancelBubble(e) {
        var e = e || window.event;
        if (e.stopPropagation()) {
            e.stopPropagation();
        } else {
            e.cancelBubble = true;
        }
    },
    // 阻止默认行为
    preventDefaultEvent(e) {
        var e = e || window.event;
        if (e.preventDefault) {
            e.preventDefault();
        } else {
            e.returnValue = false;
        }
    },
    // router跳转
    routerPush(router, path) {
        router.push(path);
    },
    // 获取可视区域宽高度
    getViewportSize() {
        if (window.innerHeight) {
            return {
                width: window.innerWidth,
                height: window.innerHeight
            }
        } else {
            if (document.compatMode === 'BackCompat') {
                return {
                    width: document.body.clientWidth,
                    height: document.body.clientHeight
                }
            } else {
                return {
                    width: document.documentElement.clientWidth,
                    height: document.documentElement.clientHeight
                }
            }
        }
    },
    //函数防抖
    debounce(func, wait = 68, immediate = true) {
        let timer
        return (...args) => {
            let context = this
            if (timer) clearTimeout(timer)
            if (immediate) {
                let callNow = !timer
                timer = setTimeout(() => {
                    timer = null
                }, wait)
                if (callNow) func.apply(context, args)
            } else {
                timer = setTimeout(() => {
                    func.apply(context, args)
                }, wait)
            }
        }
    }
}

export default utils